/*
 * --------------------------------------------------------------
 * Copyright ©  2015 Hangzhou DtDream Technologies Co.,Ltd.
 * --------------------------------------------------------------
 *      Product: 协同数治
 *  Module Name: design
 * Date Created: 2022/12/16
 *  Description: TODO
 * --------------------------------------------------------------
 * Modification History
 * DATE            NAME            Description
 * --------------------------------------------------------------
 * 2022/12/16       yewj            TODO
 * --------------------------------------------------------------
 */

package com.dtdream.pattern.single.type6;

/**
 * 静态内部类实现单例模式
 * 推荐使用
 * @Author: yewj
 **/
public class SingleSix {
    public static void main(String[] args) {

        Singleton instanceOne = Singleton.getInstance();
        Singleton instanceTwo = Singleton.getInstance();
        System.out.println(instanceOne == instanceTwo);
        System.out.println("instanceOne.hashCode() = " + instanceOne.hashCode());
        System.out.println("instanceTwo.hashCode() = " + instanceTwo.hashCode());
    }
}

// 在加载Singleton的时候，不会加载内部类，而且类在加载的时候是线程安全的
class Singleton {
    private static volatile Singleton instance;

    private Singleton() {}

    private static class SingletonInstance {
        private static final  Singleton INSTANCE = new Singleton();
    }

    public static synchronized Singleton getInstance() {
        return SingletonInstance.INSTANCE;
    }
}
